﻿using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;
using System.ServiceModel.Web;
using System.ServiceModel;

namespace MLBService
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            //The WebServiceHost handles binding and endpoint configuration automatically
            RouteTable.Routes.Add(new ServiceRoute("mlb", new WebServiceHostFactory(), typeof(MlbService)));
        }

        //Not used, but this is the approach to manually wire up the service
        private void WireManually()
        {
            //Service interface is defined by IMlb and implemented by MlbService

            //Create the base address and binding to be used with the service   
            String baseAddress = "http://localhost:51985/MlbService";
            BasicHttpBinding httpBinding = new BasicHttpBinding();

            //Create host and add endpoint
            ServiceHost host = new ServiceHost(typeof(MlbService));
            host.AddServiceEndpoint(typeof(MlbService), httpBinding, baseAddress);
            host.Open();
        }

        private void WireManuallySimplified()
        {
            WebServiceHost host = new WebServiceHost(typeof(MlbService), 
                new Uri("http://localhost:51985/MlbService"));
            host.Open();
            //wait for your guests to arrive...
        }

        //This is the kind of crap you run into when doing JSONP over localhost... 
        //protected void Application_BeginRequest(object sender, EventArgs e)
        //{
        //    if (Request.Headers["Host"].Split(':')[0] == "localhost")
        //    {
        //        Response.Redirect(
        //            Request.Url.Scheme
        //            + "://"
        //            + "local.rest.com"
        //            + ":" + Request.Url.Port.ToString()
        //            + Request.Url.PathAndQuery
        //            , true);
        //    }
        //}
        
    }
}
